package de.mmis.core.base.abstracttree.serializer.internal;

import java.text.NumberFormat;
import java.util.Map;

import de.mmis.core.base.abstracttree.LeafNode;
import de.mmis.core.base.abstracttree.Tree;
import de.mmis.core.base.abstracttree.serializer.Serializer;
import de.mmis.core.base.abstracttree.serializer.SerializerHelper;

/**
 * Serializer class for serializing numbers to Trees
 * 
 * @author Christian Koch
 */
class NumberSerializer implements Serializer<Number> {

	@Override
	public Tree serialize(Number num, boolean explicit,
			Map<Object, String> refmap, ClassLoader classLoader) {

		if (Double.isNaN(num.doubleValue()))
			return new LeafNode("NaN");

		if (Double.POSITIVE_INFINITY == num.doubleValue())
			return new LeafNode("Inf");

		if (Double.NEGATIVE_INFINITY == num.doubleValue())
			return new LeafNode("-Inf");

		NumberFormat numberFormat = SerializerHelper.getNumberFormat();
		String ret = numberFormat.format(num);
		if (explicit) {
			if (num instanceof Byte)
				ret += "b";
			if (num instanceof Short)
				ret += "s";
			// if (num instanceof Integer)
			// ret += "i";
			if (num instanceof Long)
				ret += "l";
			if (num instanceof Float)
				ret += "f";
			// if (num instanceof Double)
			// ret += "d";
		}
		return new LeafNode(ret);
	}
}
